package com.zy.bootdemo.service.impl;

import com.yq.superMarket.test.MD5Util;
import com.zy.bootdemo.entity.UserEntity;
import com.zy.bootdemo.mapper.UserMapper;
import com.zy.bootdemo.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;
    @Override
    public boolean login(UserEntity userEntity, HttpSession session) {
        //校验用户名和密码
        if (userEntity == null || userEntity.getPassword() == null || userEntity.getPassword() == ""
        || userEntity.getUsername() == null || userEntity.getUsername() == "")
        return false;
        UserEntity userData = userMapper.login(userEntity.getUsername());
        if (userData != null && userData.getPassword() != null && userData.getPassword().equals(MD5Util.getDigest(userEntity.getPassword()))) {
            session.setAttribute("curUser",userData);
            return true;
        }


        return false;
    }

    @Override
    public boolean updatePwd(String oldPassword, String newPassword, String rePassword,UserEntity userEntity) {
        oldPassword = MD5Util.getDigest(oldPassword);
        if(userEntity.getPassword().equals(oldPassword) && newPassword.equals(rePassword)){
            newPassword = MD5Util.getDigest(newPassword);
            return userMapper.updatePwd(userEntity.getId(),newPassword) !=0;
        }
        return false;
    }


}

